package com.ftwj.demo.message.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ftwj.demo.message.SendInterface;
import com.ftwj.demo.modular.dao.MessageDao;
import com.ftwj.demo.modular.dao.MessageUserDao;
import com.ftwj.demo.modular.entity.MessageBean;
import com.ftwj.demo.modular.entity.MessageUserBean;
import com.ftwj.demo.global.shiro.ShiroUtil;

@Component
public class PlatformSend implements SendInterface<MessageBean> {
	
	@Autowired
	private MessageDao msgDao;
	@Autowired
	private MessageUserDao msgUserDao;
	
	/**
	 * <p>Title: send</p>  
	 * <p>Description: 平台消息发送</p>  
	 * @param PlatfromEntity
	 * @author HeYang
	 * @date 2020年4月9日  
	 * @version 1.0
	 */
	public Integer send(MessageBean entity) {
		//消息主体保存
		Integer shiroUser = ShiroUtil.getUserId();
		entity.setCreateUser(shiroUser).setCreateTime(new Date()).setState(1);
		Integer msgId = msgDao.insertAndGetId(entity);
		List<Integer> userIdList = entity.getUserIdList();
		//保存消息接收人
		if(userIdList != null) {
			for (Integer userId : userIdList) {
				MessageUserBean msgUser = new MessageUserBean();
				msgUser.setMessId(msgId).setUserId(userId);
				msgUserDao.insert(msgUser);
			}
		}
		return 0;
	}
	
}
